<template>
	<view>
		<view style="padding: 30rpx;">
			 <u--form>
				<u-form-item
					label="是否显示教练路线" 
					borderBottom
					labelWidth="180"
					 
				>
					<u-row justify="end" style=" width: 100%;">
						<u-col span="2" offset="10">
							<u-switch  inactiveColor='#cfcfcf' v-model="showTeachWays" @change="change"></u-switch>
						</u-col>
					</u-row>
				</u-form-item>
				 <u-form-item
				 label="重置指令语音"
				 borderBottom
				 labelWidth="180">
					 <u-row justify="end" style=" width: 100%;">
					 	<u-col span="4" offset="10">
					 		<u-button size="small" @click="resetAudio" type="primary" text="重置"></u-button>

					 	</u-col>
					 </u-row>
				 </u-form-item>
			 </u--form>
		</view>
		
	</view>
	
</template>

<script>
	let that
	export default {
		data() {
			return {
				showTeachWays:false
			}
		},
		onShow() {
			let showTeachWays = uni.getStorageSync('showTeachWays')
			if(showTeachWays){
				this.showTeachWays = showTeachWays
			}
		},
		onLoad(){
			that = this
		},
		methods: {
			resetAudio(){
				uni.hideLoading()
				uni.showLoading({
					title:'初始化中',
					mask:true
				})
				let voiceCommand = uni.getStorageSync('voiceCommand')
				if(voiceCommand){
					function createIterator(items) {
					    var i = 0;
					    return {
					        next: function() {
					            var done = (i >= items.length); 
								var index
					            var value = !done ? items[i++] : undefined;
					            return {
					                done: done,
					                value: value,
									index 
					            };
					        }
					    };
					}
					var iterator = createIterator(voiceCommand);
					
					let getCache = function(){
						let iter =  iterator.next()
						if(iter.done) {  
							uni.hideLoading()
							uni.$u.toast('初始化成功')
							return
						}
						let index = iter.index
						let audioPath = iter.value.audio 
						let fileName = iter.value.command
						that.$util.getFileCache(audioPath,fileName,'audio')
						console.log('缓存到滴',index)
						getCache()
					}
					getCache()
				}else{
					 mKMSCommand.list({
						dataOrderField: "id",
						dataOrderType: "asc",
						rows: 100 
					 },result=>{
						 uni.setStorageSync('voiceCommand', result.rows)
						 that.resetAudio()
					 })
				}
			},
			change(){
				console.log('this.showTeachWays',this.showTeachWays)
				uni.setStorageSync('showTeachWays',this.showTeachWays)
			}
		}
	}
</script>

<style>
	page{
		background-color: #fff;
	}
</style>
